import Taro from '@tarojs/taro'
/** 判断是否是ios */
function isIos(): boolean {
  return !!navigator.userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)
}
/** 判断是否是安卓 */
function isAndroid(): boolean {
  /* istanbul ignore next */
  return /android/.test(navigator.userAgent.toLowerCase())
}
/** 判断是否是手机号 */
function JudgingMobile(phoneMum: string): boolean {
  return /^[1][3,4,5,6,7,8][0-9]{9}$/.test(phoneMum)
}
/** 验证密码是否合格 */
function JudgingPwd(pwdReg: string): boolean {
  return /[^\n\r]/.test(pwdReg)
}

//截取手机号
function cutphone(value: string): string {
  value = value.replace(/[^\d]/g, '')
  if (value.length > 11) {
    value = value.slice(0, 11)
  }
  return value
}
type Timers = string | number
interface TimersReturn {
  year: Timers
  month: Timers
  date: Timers
  hour: Timers
  minute: Timers
  second: Timers
}
/**
 * @param times
 * @param type
 * @example 2020-08-17 09:10:05
 */
function timeFilter(times: Date, type: string): string
function timeFilter(times: Date): TimersReturn
function timeFilter(times: Date, type?): string | TimersReturn {
  const time = new Date(times)
  const year = time.getFullYear()
  let month: Timers = time.getMonth() + 1
  let date: Timers = time.getDate()
  let hour: Timers = time.getHours()
  let minute: Timers = time.getMinutes()
  let second: Timers = time.getSeconds() // 秒 根据具体情况调用
  month = month < 10 ? '0' + month : month
  date = date < 10 ? '0' + date : date
  hour = hour < 10 ? '0' + hour : hour
  minute = minute < 10 ? '0' + minute : minute
  second = second < 10 ? '0' + second : second // 秒 根据具体情况调用
  if (type === 'YYYY-MM-DD') {
    return year + '-' + month + '-' + date
  } else if (type === 'YYYY-MM-DD HH:MM:SS') {
    return year + '-' + month + '-' + date + ' ' + hour + ':' + minute + ':' + second
  } else {
    return {
      year,
      month,
      date,
      hour,
      minute,
      second
    }
  }
}

interface CountDownReturn {
  day: number
  hour: number
  minute: number
  second: number
}
// 倒计时
function countDown(start: number, now: number): CountDownReturn {
  let dif = Math.floor((start - now) / 1000)
  if (!dif || dif <= 0) {
    return { day: 0, hour: 0, minute: 0, second: 0 }
  }
  let hour = 0
  let minute = 0
  let second = 0
  let day = 0
  const daytToSecond = 86400
  const hourToSecond = 3600
  day = Math.floor(dif / daytToSecond)
  dif = dif - day * daytToSecond
  hour = Math.floor(dif / hourToSecond)
  dif = dif - hour * hourToSecond
  minute = Math.floor(dif / 60)
  second = dif - minute * 60
  return {
    day,
    hour,
    minute,
    second
  }
}
function showToast(msg: string) {
  Taro.showToast({ title: msg, icon: 'none' })
}

function getUserInfo() {
  let _userInfo = Taro.getStorageSync('userInfo')
  _userInfo = JSON.parse(_userInfo)
  return _userInfo
}

export {
  isIos,
  isAndroid,
  JudgingMobile,
  JudgingPwd,
  cutphone,
  timeFilter,
  countDown,
  CountDownReturn,
  showToast,
  getUserInfo
}
